package cn.blu10ph;

import cn.blu10ph.tools.*;
import cn.blu10ph.git.*;
import cn.blu10ph.git.index.*;

import java.io.*;
import java.util.LinkedList;
import java.util.Map;

public class GetGitObjects {
    public static void main(String[] args) {
        String path = System.getProperty("user.dir");
        if(args.length!=0){
            path = args[0];                                                      //获取第一个参数为工作目录,例"F:\\blu10ph\\Java\\getGitObjects\\test";
        }
        File file = new File(path);
        if (!file.exists() || !file.isDirectory()) {
            System.out.println("工作的目录不存在:"+path);
            return;
        }
        System.out.println("工作的目录:"+path);

        Boolean find_sub_path = false;
        Boolean only_out = false;
        if (!(new File(path+"/.git")).exists()){
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(path + "\\" + filelist[i]+"\\.git");
                if (readfile.exists()) {
                    find_sub_path = true;
                    break;
                }
            }
        }
        if (args.length > 1) {
            find_sub_path = true;
        }
        if (args.length > 1) {
            find_sub_path = true;
        }
        if(args.length>2){
            only_out = true;
        }
        if(find_sub_path){
            System.out.println("多工作的目录");
        }
        if(only_out){
            System.out.println("工作目录只读");
        }
        if (find_sub_path) {
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                File readfile = new File(path + "\\" + filelist[i]);
                if (readfile.isDirectory() && (new File(path + "\\" + filelist[i]+"/.git")).exists()) {
                    work(path + "\\" + filelist[i], only_out);
                }
            }
        }else{
            work(path, only_out);
        }
    }
    public static void work(String path,Boolean only_out) {
        System.out.println("处理目录:"+path);

        GitUtils gitTools = new GitUtils(path);                                     //实例化工具类

        GitIndex gitIndex = gitTools.getGitIndex();                                 //获取对象
        int indexNum = 0;
        try{
            indexNum = gitIndex.getIndexLenght();                                   //获取列表长度
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        System.out.println("GitIndexLenght:"+indexNum+";");
        if(indexNum==0){
            return;
        }
        Map<String , GitIndexItemMini> fileNameList = gitIndex.getIndexItemList();              //获取文件名列表

        if(only_out){
            for (Map.Entry<String, GitIndexItemMini> entry : fileNameList.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue().getPath()+";");
            }
            return;
        }

        LinkedList<String> objectHashList = gitTools.getObjectHashs();              //获取文件列表
        int num = objectHashList.size();                                            //获取列表长度
        for (int i=0;i<num;i++) {
            System.out.println((i + 1) + "/" + num);                                      //输出进度
            String objectHash = objectHashList.get(i);                              //获取hash
            System.out.println(objectHash);                                     //输出hash
            GitIndexItemMini indexItem = fileNameList.get(objectHash);                  //获取索引项
            String pathName = indexItem==null?"":indexItem.getPath();               //获取路径
            String fileName = "";
            int start = pathName.lastIndexOf("/");
            if (start != -1 ) {
                fileName = pathName.substring(start + 1);
            }
            Boolean cat_flg = gitTools.catFile(objectHash, path);                     //获取对象
            if(!cat_flg){                                                    //如果对象为空
                System.out.println(objectHash+":读取失败;");
                continue;
            }
            Long fileLength = new Long(0);
            File f = new File(path+"/"+objectHash);
            if (f.exists() && f.isFile()){
                fileLength = f.length();
            }else{
                System.out.println(objectHash+":文件读取失败;");
                continue;
            }
            if("".equals(fileName)){
                int fileHeadLength = 1024;
                byte[] fileData = new byte[fileHeadLength];
                try {
                    FileInputStream iis = new FileInputStream(f);
                    fileHeadLength = iis.read(fileData, 0, fileHeadLength);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String fileType = FileType.getFileType(fileData);
                fileName = (objectHash+(fileType==null?"":("."+fileType)));
            }else{
                try {
                    Boolean rename_flg = GitUtils.renameFile(path,objectHash,fileName);
                    if(!rename_flg){
                        System.out.println("重命名文件失败;"+objectHash+"->"+fileName);    //输出概要信息
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println(fileName+";"+fileLength);    //输出概要信息
        }
    }
}